Ext.define('System.controller.main.Main', {
	extend : 'Ext.app.Controller',
	refs : [{
		ref : 'sys_navigation',
		selector : 'sys_navigation'
	}, {
		ref : 'sys_container',
		selector : 'sys_container'
	}],
	init : function() {
		var me = this;
		this.control({
			'viewport' : {
				render : me.onRender
			},
			scope : me
		});
	},
	onRender : function() {
		var me = this;
		
		Ext.Ajax.request({
	        url : 'resource/root',  
	        method : 'POST',
	        success : function(form, action) {
	            var json = Ext.JSON.decode(form.responseText);  
                if(json.success){
                	var conf= [];
                	for(var i=0;i<json.data.length;i++){
                		var d = json.data[i];
                		conf.push({
        					name:d.title,
        					id:d.functionid,
        					img:'images/menu/'+d.icon+'.png',
        					handler:function(){
        						me.addNavigation(this.id);
        					}
        				});
                	}
                	new fisheye({
        				items:conf
        			});
                	me.addNavigation(conf[0].id);
                }
	        } 
	    });
	},
	addNavigation:function(id){
		var me = this;
		me.getSys_navigation().removeAll();
		Ext.Ajax.request({
			url : 'resource/root/'+id,// 获取面板的地址
			method : 'POST',
			callback : function(options, success, response) {
				var json = Ext.JSON.decode(response.responseText);
				var node = null,tree=null;
				Ext.each(json.data, function(data) {
					if(node != data.parent){
						tree = Ext.create("Ext.tree.Panel", {
							title : data.title,
							iconCls : data.icon,
							useArrows : true,
							autoScroll : true,
							rootVisible : false
						});
						tree.on('itemclick', me.onTreeItemClick, me);
						me.getSys_navigation().add(tree);
						node = data.functionid;
					}else{
						var node_ = tree.getRootNode().appendChild({
					        id : data.functionid,
					        text : data.title,
					        iconCls : data.icon,
					        leaf : true
					    });
						node_.location = data.location;
					}
				});
			}
		});
	},
	onTreeItemClick : function(view, node) {
		var mainPanel = this.getSys_container();
		var me=this,url = node.location;
		var tab = mainPanel.down('[id="'+node.data.id+'"]');//mainPanel.getComponent(functionid);
		if(!tab){
			var conf = {
				id: node.data.id,
				title: node.data.text,
				icon: node.data.iconCls,
	       		closable: true
	       	};
			if (url.indexOf('/') >=0) {
				url = '<iframe scrolling="auto" name="mainFrame" frameborder="0" width="100%" height="100%" src=' + url + '></iframe>';
				conf.html = url;
			} else { //控制名称和控件xtype
				var tmp = url.split("|");
				var xtype;
				if (tmp.length>1) {
					var controllersName = tmp[0].split(",");
					for(var i=0;controllersName != null && i<controllersName.length;i++){
						controllerName = controllersName[i];
						var controller = me.getController(controllerName);
						if (!controller.isInit) {
							controller.init();
						}
					}
					xtype = tmp[1];
				} else {
					xtype = url;
				}
				conf.xtype = xtype;
			}
			tab = mainPanel.add(conf);
		}
		mainPanel.setActiveTab(tab);
	}
});